<template>
  <div class="app-container">
    <el-alert
      title="Bpmn流程设计器，仅供调试使用，非专业用户请勿使用"
      type="error"
      :closable="false"
      class="mt10 mb10"
      show-icon
    />
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="88px">
      <el-form-item label="流程KEY" prop="key">
        <el-input
          v-model="queryParams.key"
          clearable
          placeholder="请输入流程KEY"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          placeholder="请输入名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['workflow:modeler']"
          icon="el-icon-plus"
          size="mini"
          type="primary"
          @click="onlineDrawingProcess"
        >
          在线绘制流程
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['workflow:modeler']"
          icon="el-icon-plus"
          size="mini"
          type="primary"
          @click="handleImport"
        >
          部署流程
        </el-button>
      </el-col>

      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>


    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="流程ID" prop="id" />
      <el-table-column align="center" label="流程KEY" prop="key" />
      <el-table-column align="center" label="流程名称" prop="name" />
      <el-table-column label="流程版本" align="center">
        <template slot-scope="scope">
          <el-tag size="medium">
            v{{ scope.row.version }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="部署时间" prop="deploymentTime" />
      <el-table-column align="center" label="部署ID" prop="deploymentId" />

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.suspendState === 1" type="success">
            激活
          </el-tag>
          <el-tag v-if="scope.row.suspendState === 2" type="warning">
            挂起
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['workflow:modeler']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="onlineModificationProcess(scope.row)"
          >
            查看
          </el-button>
          <el-button
            v-hasPermi="['workflow:modeler']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="suspendOrActiveApply(scope.row)"
          >
            {{ scope.row.suspendState == 1 ? '挂起' : '激活' }}
          </el-button>

          <el-button
            v-hasPermi="['workflow:modeler']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      :total="total"
      @pagination="getList"
    />
    <!--bpmnjs在线流程设计器-->
    <el-dialog
      :visible.sync="modelVisible"
      append-to-body
      title="流程图"
      fullscreen
      @close="modelCancel"
    >
      <div style="position:relative;height: 100%;">
        <process-designer :deploy-id="deployId" />
      </div>
    </el-dialog>


    <el-dialog :title="upload.title" :visible.sync="upload.open" append-to-body width="400px">
      <el-upload ref="upload" :action="upload.url" :auto-upload="false" :disabled="upload.isUploading"
                 :headers="upload.headers"
                 :limit="1" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess"
                 accept=".bpmn, .bar, .zip" drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div slot="tip" class="el-upload__tip" style="color:red">
          提示：仅允许导入“bpmn”、“bar”或“zip”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">
          确 定
        </el-button>
        <el-button @click="upload.open = false">
          取 消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ProcessDesigner from '../designer/ProcessDesigner'
import {delDefinition, listDefinition, suspendOrActiveApply} from "@/api/workflow/definition"
import {getToken} from "@/utils/auth"
import mixin from "../../form/TduckFormMixin"

export default {
  name: "ProcessDefinition",
  components: {
    ProcessDesigner
  },
  mixins: [mixin],
  data() {
    return {
      modelVisible: false,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // VIEW表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      src: "",
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        id: null,
        rev: null,
        name: null,
        type: null
      },
      deployId: null,
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_API_ROOT + "/process/definition/deployment"
      }
    }
  },
  created() {
    this.getList()
    //开放一个窗口控制函数
    window['setModelVisible'] = (val) => {
      this.modelVisible = val
      if (this.modelVisible === false) {
        this.handleQuery()
      }
    }
  },
  methods: {
    /** 查询Definition列表 */
    getList() {
      this.loading = true
      listDefinition(this.queryParams).then((response) => {
        this.list = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    suspendOrActiveApply(row) {
      let suspendOrActive = row.suspendState === '2' ? '激活' : '挂起'
      this.$confirm('确认要' + suspendOrActive + '流程定义吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        let data = {"id": row.id, "suspendState": row.suspendState}
        return suspendOrActiveApply(data)
      }).then(() => {
        this.getList()
        this.msgSuccess("转换成功")
      }).catch(function () {
      })

    },
    handleImport() {
      this.upload.title = "上传模型图"
      this.upload.open = true
    },
    onlineDrawingProcess() {
      this.modelVisible = true
    },
    onlineModificationProcess(data) {
      this.modelVisible = true
      this.deployId = data.deploymentId
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.msgSuccess('部署成功')
      this.getList()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除编号为"' + row.key + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delDefinition(row.deploymentId)
      }).then(() => {
        this.getList()
        this.msgSuccess("删除成功")
      }).catch(function () {
      })
    },
    modelCancel() {
      this.getList()
    }

  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  height: 100%;
  max-height: 100% !important;
}
</style>
